package com.pingtai.xinlang.action;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

import com.pingtai.ajax.entity.IndentTarget_163;
import com.pingtai.util.Utils;
import com.pingtai.xinlang.login.entity.LoginUser;
import com.pingtai.xinlang.login.entity.XinlangLoginCookie;
import com.pingtai.xinlang.login.login.WeiboLoginService;

public class XinlangAjaxAction {
	
	private String url;
	private WeiboLoginService weiboLoginService;
	private Integer result;
	private IndentTarget_163 indentTarget_163;
	
	public IndentTarget_163 getIndentTarget_163() {
		return indentTarget_163;
	}

	public void setIndentTarget_163(IndentTarget_163 indentTarget_163) {
		this.indentTarget_163 = indentTarget_163;
	}

	public Integer getResult() {
		return result;
	}

	public void setResult(Integer result) {
		this.result = result;
	}

	public WeiboLoginService getWeiboLoginService() {
		return weiboLoginService;
	}

	public void setWeiboLoginService(WeiboLoginService weiboLoginService) {
		this.weiboLoginService = weiboLoginService;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}
	
	public String getRenrenGzRenqiTarget() throws Exception{
		LoginUser loginUser = weiboLoginService.getLastUseLoginUser(2);
		HttpClient client = null;
		client = new DefaultHttpClient();
		HttpGet getMethod = new HttpGet(url);  
		getMethod.setHeader("Content-Type", "application/x-www-form-urlencoded");  
        getMethod.setHeader("Accept-Language","zh-cn");  
        getMethod.setHeader("Connection","Keep-Alive");  
        getMethod.setHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1 QIHU 360SE");  
        getMethod.setHeader("Host","page.renren.com");  
        getMethod.setHeader("Cookie",loginUser.getCookies());  
        HttpResponse response = client.execute(getMethod);
        InputStream in = response.getEntity().getContent();
        BufferedInputStream bin = new BufferedInputStream(in);
        String msg = Utils.InputStreamTOString(in, "utf-8");
		msg = Utils.unicodeToString(msg);
		indentTarget_163 = weiboLoginService.parseRenrenGzRenqiHtml(msg);
		return "success";
	}
	
	public String getRenrenRenqiTarget() throws Exception{
		LoginUser loginUser = weiboLoginService.getLastUseLoginUser(2);
		HttpClient client = null;
		client = new DefaultHttpClient();
		HttpGet getMethod = new HttpGet(url);  
		getMethod.setHeader("Content-Type", "application/x-www-form-urlencoded");  
        getMethod.setHeader("Accept-Language","zh-cn");  
        getMethod.setHeader("Connection","Keep-Alive");  
        getMethod.setHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1 QIHU 360SE");  
        getMethod.setHeader("Host","www.renren.com");  
        getMethod.setHeader("Cookie",loginUser.getCookies());  
        HttpResponse response = client.execute(getMethod);
        InputStream in = response.getEntity().getContent();
        BufferedInputStream bin = new BufferedInputStream(in);
        String msg = Utils.InputStreamTOString(in, "utf-8");
		msg = Utils.unicodeToString(msg);
		indentTarget_163 = weiboLoginService.parseRenrenRenqiHtml(msg);
		return "success";
	}
	
	public String getXlZfTarget() throws Exception{
//		LoginUser loginUser = weiboLoginService.getLoginUser("mjjsojava@163.com");
		LoginUser loginUser = weiboLoginService.getLastUseLoginUser(1);
		HttpClient client = null;
//		if(!weiboLoginService.hasCookie(loginUser)||weiboLoginService.isGuoqiOfCookie(loginUser)){
//			client = new DefaultHttpClient();
//			List<XinlangLoginCookie> cookieList = weiboLoginService.getLoginCookie(loginUser);
//			client = weiboLoginService.setCookiesToClient(client, cookieList);
//			String msg = weiboLoginService.getConent(url, client);
//			//解析
//			System.out.println("已登录");
//			System.out.println(Utils.unicodeToString(msg));
//			result = 1;
//		}else{
//			client = weiboLoginService.getLoginStatus(loginUser.getName(), loginUser.getPass());
			client = new DefaultHttpClient();
			
			if(!weiboLoginService.hasCookie(loginUser)||weiboLoginService.isGuoqiOfCookie(loginUser)){
				client = weiboLoginService.getLoginStatus(loginUser.getName(), loginUser.getPass());
				System.out.println("新浪微博登陆成功！");
			}
			
			HttpGet getMethod = new HttpGet(url);  
	          
	        getMethod.setHeader("Content-Type", "application/x-www-form-urlencoded");  
	        //下面一行需要注释掉，不然返回数据是gzip压缩后的乱码  
	        //getMethod.setRequestHeader("Accept-Encoding","gzip, deflate");  
	        getMethod.setHeader("Accept-Language","zh-cn");  
	        getMethod.setHeader("Connection","Keep-Alive");  
	        getMethod.setHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1 QIHU 360SE");  
	        
	        if(url.indexOf("e.weibo.com")!=-1){
	        	getMethod.setHeader("Host","e.weibo.com"); 
	        }else{
	        	getMethod.setHeader("Host","weibo.com"); 
	        }
	        getMethod.setHeader("Cookie",loginUser.getCookies());  
	        HttpResponse response = client.execute(getMethod);
	        InputStream in = response.getEntity().getContent();
	        BufferedInputStream bin = new BufferedInputStream(in);
	        String msg = Utils.InputStreamTOString(in, "utf-8");
	        //			String msg = weiboLoginService.getConent(url, client);
			//解析
			msg = Utils.unicodeToString(msg);
			if(msg.indexOf("<script>STK && STK.pageletM && STK.pageletM.view({\"pid\":\"pl_content_weiboDetail")!=-1){
				msg = msg.substring(msg.indexOf("<script>STK && STK.pageletM && STK.pageletM.view({\"pid\":\"pl_content_weiboDetail"),
						msg.indexOf("<script>STK && STK.pageletM && STK.pageletM.view({\"pid\":\"pl_content_Base"));
				msg = msg.replace("<script>STK && STK.pageletM && STK.pageletM.view(", "");
				msg = msg.replace(")</script>", "");
				Map map = Utils.getMapFromJson(msg);
				String html = (String) map.get("html");
				indentTarget_163 = weiboLoginService.parseXlZfHtml(html);
			}
			
			
			if(msg.indexOf("<script>FM.view({\"ns\":\"pl.content.weiboDetail.index")!=-1){
				if(msg.indexOf("<script>FM.view({\"ns\":\"pl.third.recommend.index")!=-1){
					if(msg.indexOf("<script>FM.view({\"ns\":\"pl.third.recommend.index")<msg.indexOf("<script>FM.view({\"ns\":\"pl.content.weiboDetail.index")){
						msg = msg.substring(msg.indexOf("<script>FM.view({\"ns\":\"pl.content.weiboDetail.index"), msg.indexOf("</html>"));
					}else{
						msg = msg.substring(msg.indexOf("<script>FM.view({\"ns\":\"pl.content.weiboDetail.index"), msg.indexOf("<script>FM.view({\"ns\":\"pl.third.recommend.index"));
					}
					
				}else if(msg.indexOf("<script>FM.view({\"ns\":\"\",\"domid\":\"Pl_Core_RightTextSingle")!=-1){
					if(msg.indexOf("<script>FM.view({\"ns\":\"\",\"domid\":\"Pl_Core_RightTextSingle")<msg.indexOf("<script>FM.view({\"ns\":\"pl.content.weiboDetail.index")){
						msg = msg.substring(msg.indexOf("<script>FM.view({\"ns\":\"pl.content.weiboDetail.index"), msg.indexOf("</html>"));
					}else{
						msg = msg.substring(msg.indexOf("<script>FM.view({\"ns\":\"pl.content.weiboDetail.index"), msg.indexOf("<script>FM.view({\"ns\":\"\",\"domid\":\"Pl_Core_RightTextSingle"));
					}
					
					
				}
				msg = msg.replace("<script>FM.view(", "");
				msg = msg.replace(")</script>", "");
				Map map = Utils.getMapFromJson(msg);
				String html = (String) map.get("html");
				indentTarget_163 = weiboLoginService.parseXlZfHtml(html);
			}
			
			
//		}
		return "success";
		
	}
	
	
	public String getXlFanTarget() throws Exception{
		LoginUser loginUser = weiboLoginService.getLastUseLoginUser(1);
		HttpClient client = null;
		//未登录client
		client = weiboLoginService.getNoLoginClient();
		String noLoginMsg = weiboLoginService.getConent(url, client,"UTF-8");
		if(noLoginMsg==null){
			noLoginMsg="";
		}
		noLoginMsg = Utils.unicodeToString(noLoginMsg);
		if(noLoginMsg.indexOf("FM.view({\"ns\":\"pl.header.head.index")!=-1){
			
			String name = weiboLoginService.parseXlFanNameHtml(noLoginMsg);
			noLoginMsg = noLoginMsg.substring(noLoginMsg.indexOf("<script>FM.view({\"ns\":\"pl.header.head.index"),
					noLoginMsg.indexOf("<script>FM.view({\"ns\":\"pl.nav.index"));
			noLoginMsg = noLoginMsg.replace("<script>FM.view(", "");
			noLoginMsg = noLoginMsg.replace(")</script>", "");
			Map map = Utils.getMapFromJson(noLoginMsg);
			String html = (String) map.get("html");
			indentTarget_163 = weiboLoginService.parseXlFanHtml(html);
			indentTarget_163.setName(name);
		}else if(noLoginMsg.indexOf("STK && STK.pageletM && STK.pageletM.view({\"pid\":\"pl_content_litePersonInfo")!=-1){
			String name = weiboLoginService.parseXlFanNameHtml(noLoginMsg);
			
			noLoginMsg = noLoginMsg.substring(noLoginMsg.indexOf("STK && STK.pageletM && STK.pageletM.view({\"pid\":\"pl_content_litePersonInfo"),
					noLoginMsg.indexOf("<script>STK && STK.pageletM && STK.pageletM.view({\"pid\":\"pl_common_relations"));
			noLoginMsg = noLoginMsg.replace("STK && STK.pageletM && STK.pageletM.view(", "");
			noLoginMsg = noLoginMsg.replace(")</script>", "");
			Map map = Utils.getMapFromJson(noLoginMsg);
			String html = (String) map.get("html");
			indentTarget_163 = weiboLoginService.parseXlFanNumHtml(html);
			indentTarget_163.setName(name);
		}else{
			client = new DefaultHttpClient();
			if(!weiboLoginService.hasCookie(loginUser)||weiboLoginService.isGuoqiOfCookie(loginUser)){
				client = weiboLoginService.getLoginStatus(loginUser.getName(), loginUser.getPass());
				System.out.println("新浪微博登陆成功！");
			}
			HttpGet getMethod = new HttpGet(url);  
	          
	        getMethod.setHeader("Content-Type", "application/x-www-form-urlencoded");  
	        //下面一行需要注释掉，不然返回数据是gzip压缩后的乱码  
	        //getMethod.setRequestHeader("Accept-Encoding","gzip, deflate");  
	        getMethod.setHeader("Accept-Language","zh-cn");  
	        getMethod.setHeader("Connection","Keep-Alive");  
	        getMethod.setHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1 QIHU 360SE");  
	        getMethod.setHeader("Host","weibo.com");  
	        getMethod.setHeader("Cookie",loginUser.getCookies()); 
	        HttpResponse response = client.execute(getMethod);
	        InputStream in = response.getEntity().getContent();
	        BufferedInputStream bin = new BufferedInputStream(in);
	        String msg = Utils.InputStreamTOString(in, "utf-8");
	        //			String msg = weiboLoginService.getConent(url, client);
			//解析
//			client = weiboLoginService.getLoginStatus(loginUser.getName(), loginUser.getPass());
//			String msg = weiboLoginService.getConent(url, client);
			//解析
			msg = Utils.unicodeToString(msg);
			if(msg.indexOf("<script>FM.view({\"ns\":\"pl.header.head.index")!=-1){
				msg = msg.substring(msg.indexOf("<script>FM.view({\"ns\":\"pl.header.head.index"),
						msg.indexOf("<script>FM.view({\"ns\":\"pl.nav.index"));
				msg = msg.replace("<script>FM.view(", "");
				msg = msg.replace(")</script>", "");
				Map map = Utils.getMapFromJson(msg);
				String html = (String) map.get("html");
				indentTarget_163 = weiboLoginService.parseXlFanHtml(html);
			}
		}
		
		
		
		return "success";
		
	}
	
	public String getIndentTargetInfo_qq_kj_fan() throws Exception{
		HttpClient client = null;
		client = new DefaultHttpClient();
		HttpGet getMethod = new HttpGet(url);  
		getMethod.setHeader("Content-Type", "application/x-www-form-urlencoded");  
        getMethod.setHeader("Accept-Language","zh-CN,zh;q=0.8");  
        getMethod.setHeader("Connection","Keep-Alive");  
        getMethod.setHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1");  
        getMethod.setHeader("Host","user.qzone.qq.com");  
        HttpResponse response = client.execute(getMethod);
        InputStream in = response.getEntity().getContent();
        BufferedInputStream bin = new BufferedInputStream(in);
        String msg = Utils.InputStreamTOString(in, "utf-8");
		msg = Utils.unicodeToString(msg);
		
		indentTarget_163 = getIndentTargetInfo_qq_kj_fan(msg);
		List uidList = Utils.getDigit(url);
		String uid = "";
		if(uidList!=null&&uidList.size()>0){
			Long l =  (Long) uidList.get(0);
			uid = l.toString();
		}
		HttpGet getMethod2 = new HttpGet("http://r.qzone.qq.com/cgi-bin/tfriend/cgi_like_check_and_getfansnum.cgi?uin="+uid+"&mask=3");
		getMethod2.setHeader("Content-Type", "application/x-www-form-urlencoded");  
		getMethod2.setHeader("Accept-Language","zh-CN,zh;q=0.8");  
		getMethod2.setHeader("Connection","Keep-Alive");  
		getMethod2.setHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1");  
		getMethod2.setHeader("Host","r.qzone.qq.com");  
        response = client.execute(getMethod2);
        in = response.getEntity().getContent();
        bin = new BufferedInputStream(in);
        msg = Utils.InputStreamTOString(in, "utf-8");
		msg = Utils.unicodeToString(msg);
		msg = msg.replace("_Callback(", "");
		msg = msg.replace(");", "");
		Map m = Utils.getMapFromJson(msg);
		Map m2 = (Map) m.get("data");
		Integer num = (Integer) m2.get("total");
		indentTarget_163.setNumber(num.toString());
		return "success";
	}
	
	 public IndentTarget_163 getIndentTargetInfo_qq_kj_fan(String html) throws IOException, ParserException{
	    	
			Parser parser = Parser.createParser(html, "utf-8");
			NodeFilter nameFilter = new  TagNameFilter("title");
			String name = "";
			NodeList nodes = parser.extractAllNodesThatMatch(nameFilter);
			if (nodes != null) {
				for (int i = 0; i < nodes.size(); i++) {
					Node textnode = (Node) nodes.elementAt(i);
					name = textnode.toHtml();
					name = Utils.NoString(name);
					name = name.substring(name.indexOf("认证空间 -- ")+8, name.indexOf("["));
				}
			}
			
			IndentTarget_163 indentTarget_163 = new IndentTarget_163();
			indentTarget_163.setName(name);
			return indentTarget_163;
	    }

}
